﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HWEngine;

namespace HandWriter
{
	public partial class FontsManagerDialog : Form, IDocument
	{
		private LetterSetRepository _fonts;
		private FontEditorDialog _current = null;

		public FontsManagerDialog(LetterSetRepository fonts)
		{
			_fonts = fonts;
			InitializeComponent();
			fontsSelectionList1.SetFonts(fonts);
		}

		private void fontsSelectionList1_OnFontItemSelected(object sender, EventArgs e)
		{
			SetSelectedPage();
		}

		private void SetSelectedPage()
		{
			Cursor.Current = Cursors.WaitCursor;

			//close current font editor
			if (_current != null)
			{
				_current.Close();
				_current.Dispose();
			}

			_current = new FontEditorDialog(_fonts.Get(fontsSelectionList1.SelectedFontName));
			_current.TopLevel = false;
			_current.MdiParent = null;
			_current.FormBorderStyle = FormBorderStyle.None;
			_current.Dock = DockStyle.Fill;
			panel1.Controls.Add(_current);
			_current.Visible = true;
			_current.BringToFront();
			_current.Focus();

			Cursor.Current = Cursors.Arrow;

			Text = "Fonts Manager - " + fontsSelectionList1.SelectedFontName;
		}

		private void FontsManager_Load(object sender, EventArgs e)
		{

		}

		private void btnCreateFont_Click(object sender, EventArgs e)
		{
			_fonts.Create();
			fontsSelectionList1.SetFonts(_fonts);
		}

		private void btnSaveAll_Click(object sender, EventArgs e)
		{
			SaveAll();
		}

		private void btnDeleteFont_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show(
				string.Format("Do you really want to delete font '{0}'?", fontsSelectionList1.SelectedFontName),
				"Hand Writer",
				MessageBoxButtons.YesNo,
				MessageBoxIcon.Warning,
				MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
			{
				_fonts.DeleteFont(fontsSelectionList1.SelectedFontName);
				fontsSelectionList1.SetFonts(_fonts);
				SetSelectedPage();
			}
		}

		private void FontsManager_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (_current != null)
			{
				_current.Close();
				_current.Dispose();
				_current = null;
			}
		}

		#region IDocument

		public bool PrintingAllowed
		{
			get { return false; }
		}

		public bool ExportAllowed
		{
			get { return false; }
		}

		public bool SaveAllowed
		{
			get { return false; }
		}

		public bool SaveAsAllowed
		{
			get { return false; }
		}

		public bool SaveAllAllowed
		{
			get { return true; }
		}

		public Form Form
		{
			get { return this; }
		}

		public void Print()
		{
			throw new NotImplementedException();
		}

		public void Save()
		{
			throw new NotImplementedException();
		}

		public void SaveAs()
		{
			throw new NotImplementedException();
		}

		public void SaveAll()
		{
			_fonts.Save();
			fontsSelectionList1.SetFonts(_fonts);
		}

		public void Export()
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}
